﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace Coldairarrow.Util
{
    /// <summary>
    /// 自动适配类型
    /// 继承类互转
    /// </summary>
    public static class AutoAdaptation
    {
        /// <summary>
        /// 自动将父类对象赋值转成子类对象
        /// </summary>
        /// <typeparam name="TParent">父类型</typeparam>
        /// <typeparam name="TChild">子类型</typeparam>
        /// <param name="parent">父类对象</param>
        /// <returns>子类对象</returns>
        public static TChild AutoCopy<TParent, TChild>(TParent parent) where TChild : TParent, new()
        {
            TChild child = new TChild();
            var ParentType = typeof(TParent);
            var Properties = ParentType.GetProperties();
            foreach (var Propertie in Properties)
            {
                //循环遍历属性
                if (Propertie.CanRead && Propertie.CanWrite)
                {
                    //进行属性拷贝
                    Propertie.SetValue(child, Propertie.GetValue(parent, null), null);
                }
            }
            return child;
        }

        /// <summary>
        /// 自动将父类对象集合赋值转成子类对象集合
        /// </summary>
        /// <typeparam name="TParent">父类型</typeparam>
        /// <typeparam name="TChild">子类型</typeparam>
        /// <param name="parents">父类对象集合</param>
        /// <returns></returns>
        public static List<TChild> AutoCopy<TParent, TChild>(List<TParent> parents) where TChild : TParent, new()
        {
            List<TChild> childs = new List<TChild>();
            //遍历父类集合
            foreach (var item in parents)
            {
                childs.Add(AutoCopy<TParent, TChild>(item));
            }
            return childs;
        }

        /// <summary>
        /// 根据类型名称自动转换成类型值
        /// </summary>
        /// <param name="typeName">类型名称</param>
        /// <returns>返回类型值</returns>
        public static Type AutoConvertType(string typeName)
        {
            //声明空类型变量
            Type type = null;
            //获取已加载到此应用程序域的执行上下文中的程序集
            Assembly[] assemblyArray = AppDomain.CurrentDomain.GetAssemblies();
            //获取程序集数组长度
            int assemblyArrayLength = assemblyArray.Length;
            //遍历程序集数组寻找类型
            for (int i = 0; i < assemblyArrayLength; ++i)
            {
                type = assemblyArray[i].GetType(typeName);
                if (type != null)
                {
                    return type;
                }
            }
            for (int i = 0; (i < assemblyArrayLength); ++i)
            {
                Type[] typeArray = assemblyArray[i].GetTypes();
                int typeArrayLength = typeArray.Length;
                for (int j = 0; j < typeArrayLength; ++j)
                {
                    if (typeArray[j].Name.Equals(typeName))
                    {
                        return typeArray[j];
                    }
                }
            }
            return type;
        }

    }

    /// <summary>
    /// 用于导出时存储Excel表格对象
    /// </summary>
    public class ExcelSheet
    {
        /// <summary>
        /// Excel表行对象集合
        /// </summary>
        public List<ExcelRow> Rows { get; set; } = new List<ExcelRow>();
    }

    /// <summary>
    /// 用于导出时存储Excel表格行对象
    /// </summary>
    public class ExcelRow
    {
        /// <summary>
        /// Excel表单元格值集合
        /// </summary>
        public List<string> Cells { get; set; } = new List<string>();
    }






}
